package org.linlinjava.litemall.wx.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.util.DataUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.service.CategoryService;
import org.linlinjava.litemall.db.service.GoodsService;
import org.linlinjava.litemall.db.util.DateUtil;
import org.linlinjava.litemall.wx.util.CloneUtil;
import org.linlinjava.litemall.wx.vo.SecKillInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description: 秒杀
 * @author: Destin
 * @date: 2021-09-26
 */
@RestController
@RequestMapping("/wx/seckill")
@Validated
@Api("秒杀服务")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class WxSeckillController {

    private final CategoryService categoryService;

    private final GoodsService goodsService;

    @ApiOperation(value = "获取当日秒杀商品")
    @GetMapping("/all/list")
    public Object getSecKillAll() {
        Map<String, List<SecKillInfoVo>> returnMap = new HashMap<>();
        Map<String, List<LitemallGoods>> stringListMap = goodsService.secKillAll();
        for (Map.Entry<String, List<LitemallGoods>> entry : stringListMap.entrySet()) {
            List<LitemallGoods> value = entry.getValue();
            List<SecKillInfoVo> secKillInfoVos = CloneUtil.cloneList(value, SecKillInfoVo.class);
            secKillInfoVos.forEach(sec->{
                sec.setGoodId(sec.getId());
                sec.setCategoryName(categoryService.findById(sec.getCategoryId()).getName());
            });
            returnMap.put(entry.getKey(), secKillInfoVos);
        }
        return ResponseUtil.ok(returnMap);
    }

//    @ApiOperation(value = "获取当日秒杀商品")
//    @GetMapping("/all/list")
//    public Object getSecKillAll() {
//        Map<String, List<SecKillInfoVo>> returnMap = new HashMap<>();
//        Map<String, List<SecKillInfoVo>> resultMap = new LinkedHashMap<>();
//        Map<String, List<LitemallGoods>> stringListMap = goodsService.secKillAll();
//        for (Map.Entry<String, List<LitemallGoods>> entry : stringListMap.entrySet()) {
//            List<LitemallGoods> value = entry.getValue();
//            List<SecKillInfoVo> secKillInfoVos = CloneUtil.cloneList(value, SecKillInfoVo.class);
//            secKillInfoVos.forEach(sec->{
//                sec.setGoodId(sec.getId());
//                sec.setCategoryName(categoryService.findById(sec.getCategoryId()).getName());
//            });
//            returnMap.put(entry.getKey(), secKillInfoVos);
//        }
//        for (Map.Entry<String, List<SecKillInfoVo>> entry : returnMap.entrySet()) {
//            if (DateUtil.isBetween(entry.getValue().get(0).getStartTime(), entry.getValue().get(0).getEndTime())) {
//                resultMap.put(entry.getKey(), entry.getValue());
//            }
//        }
//        for (Map.Entry<String, List<SecKillInfoVo>> entry : returnMap.entrySet()) {
//            if (DateUtil.getDateTimeDisplayString(entry.getValue().get(0).getStartTime()).compareTo(DateUtil.currentTime()) > 0) {
//                resultMap.put(entry.getKey(), entry.getValue());
//            }
//        }
//        for (Map.Entry<String, List<SecKillInfoVo>> entry : returnMap.entrySet()) {
//            if (DateUtil.getDateTimeDisplayString(entry.getValue().get(0).getEndTime()).compareTo(DateUtil.currentTime()) < 0) {
//                resultMap.put(entry.getKey(), entry.getValue());
//            }
//        }
//        return ResponseUtil.ok(resultMap);
//    }

    @ApiOperation(value = "获取今日最新秒杀商品")
    @GetMapping("/today/list")
    public List<LitemallGoods> getSecKillNew() {
        return goodsService.secKillNew();
    }
}
